package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC821 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] shortestToChar(String s, char c) {
            TreeSet<Integer> pos = new TreeSet<>();
            int len = s.length();
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) == c) pos.add(i);
            }

            int[] ans = new int[len];
            for (int i = 0; i < len; i++) {
                Integer before = pos.floor(i), after = pos.ceiling(i);
                before = null == before ? -500000 : before;
                after = null == after ? 500000 : after;
                ans[i] = Math.min(Math.abs(i - before), Math.abs(i - after));
            }

            return ans;
        }
    }

    public void run() {
        System.out.println(Arrays.toString(new Solution().shortestToChar("loveleetcode", 'e')));
        System.out.println(Arrays.toString(new Solution().shortestToChar("aaab", 'b')));
    }

    public static void main(String[] args) throws Exception {
        LC821 an = new LC821();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
